package edu.bu.cs673.superkids123.parents.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.AbstractNavigationView;
import edu.bu.cs673.superkids123.children.model.Theme;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResource;
import edu.bu.cs673.superkids123.common.model.Child;
import edu.bu.cs673.superkids123.common.model.Child.Age;
import edu.bu.cs673.superkids123.common.model.Child.Sex;
import edu.bu.cs673.superkids123.parents.DataObjectAccess.ParentSystemManager;
import edu.bu.cs673.superkids123.parents.controller.CreateChildController;

public class CreateChildView extends AbstractNavigationView implements
		KeyListener, ActionListener {

	private JTextField nameField;
	private JButton createBtn;
	private JLabel errorMessage;
	private JComboBox ageComboBox;
	private JComboBox sexComboBox;
	private JSpinner playTimeSpinner;

	// Start with false as these will be empty
	private boolean userNamePassed = false;

	public CreateChildView(AbstractFrame mainFrame) {
		super(mainFrame);
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	protected JPanel layout() {

		JPanel userInfo = new JPanel(new GridBagLayout());
		GridBagLayout gblUserInfo = new GridBagLayout();
		gblUserInfo.columnWidths = new int[] { 75, 100, 20, 300, 75 };
		gblUserInfo.rowHeights = new int[] { 50, 50, 50, 50, 50, 50 };
		userInfo.setLayout(gblUserInfo);

		JTextArea infoMsg = new JTextArea();
		infoMsg.setWrapStyleWord(true);
		infoMsg.setLineWrap(true);
		infoMsg.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		infoMsg.setEditable(false);
		infoMsg.setBackground(userInfo.getBackground());

		if (authController.areChildrenInitialized()) {
			infoMsg.setText("Enter the following information for the new child:");
		} else {
			infoMsg.setText("Before your child can start the adventure you must create an account for him/her:");
		}

		GridBagConstraints gbcInfoMsg = new GridBagConstraints();
		gbcInfoMsg.gridx = 0;
		gbcInfoMsg.gridy = 0;
		gbcInfoMsg.gridwidth = 5;
		gbcInfoMsg.fill = GridBagConstraints.HORIZONTAL;
		userInfo.add(infoMsg, gbcInfoMsg);

		JLabel nameLabel = new JLabel("Name:");
		nameLabel.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		GridBagConstraints gbcNameLabel = new GridBagConstraints();
		gbcNameLabel.anchor = GridBagConstraints.EAST;
		gbcNameLabel.gridx = 1;
		gbcNameLabel.gridy = 1;
		userInfo.add(nameLabel, gbcNameLabel);

		nameField = new JTextField();
		nameLabel.setLabelFor(nameField);
		nameField.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
		nameField.addKeyListener(this);
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 3;
		gbc_textField.gridy = 1;
		userInfo.add(nameField, gbc_textField);
		nameField.setColumns(10);

		// Child Age
		JLabel ageLabel = new JLabel("Age ");
		ageLabel.setFont(Theme.PARENTS_FONT);
		GridBagConstraints gbcAgeNameLabel = new GridBagConstraints();
		gbcAgeNameLabel.anchor = GridBagConstraints.EAST;
		gbcAgeNameLabel.gridx = 1;
		gbcAgeNameLabel.gridy = 2;
		userInfo.add(ageLabel, gbcAgeNameLabel);

		ageComboBox = new JComboBox(Age.values());
		GridBagConstraints gbcAgeComboBox = new GridBagConstraints();
		gbcAgeComboBox.anchor = GridBagConstraints.WEST;
		gbcAgeComboBox.gridx = 3;
		gbcAgeComboBox.gridy = 2;
		userInfo.add(ageComboBox, gbcAgeComboBox);

		// Sex
		JLabel sexLabel = new JLabel("Sex ");
		sexLabel.setFont(Theme.PARENTS_FONT);
		GridBagConstraints gbcSexLabel = new GridBagConstraints();
		gbcSexLabel.anchor = GridBagConstraints.EAST;
		gbcSexLabel.gridx = 1;
		gbcSexLabel.gridy = 3;
		userInfo.add(sexLabel, gbcSexLabel);

		sexComboBox = new JComboBox(Sex.values());
		GridBagConstraints gbcSexComboBox = new GridBagConstraints();
		gbcSexComboBox.anchor = GridBagConstraints.WEST;
		gbcSexComboBox.gridx = 3;
		gbcSexComboBox.gridy = 3;
		userInfo.add(sexComboBox, gbcSexComboBox);
		
		// Play Time
		JLabel playTimePerDaylabel = new JLabel("Max play time per day ");
		playTimePerDaylabel.setFont(Theme.PARENTS_FONT);
		GridBagConstraints gbcPlayTimePerDayLabel = new GridBagConstraints();
		gbcPlayTimePerDayLabel.gridx = 1;
		gbcPlayTimePerDayLabel.gridy = 4;
		userInfo.add(playTimePerDaylabel, gbcPlayTimePerDayLabel);
		
		SpinnerModel model = new SpinnerNumberModel(0, 0, 1439, 1);
		JLabel minutes = new JLabel(" minutes");
		minutes.setFont(Theme.PARENTS_FONT);
		playTimeSpinner = new JSpinner(model);
		playTimeSpinner.setFont(Theme.PARENTS_FONT);
		JPanel playTimePanel = new JPanel(new BorderLayout());
		playTimePanel.add(playTimeSpinner, BorderLayout.WEST);
		playTimePanel.add(minutes, BorderLayout.EAST);
		GridBagConstraints gbcPlayTimeSpinner = new GridBagConstraints();
		gbcPlayTimeSpinner.anchor = GridBagConstraints.WEST;
		gbcPlayTimeSpinner.gridx = 3;
		gbcPlayTimeSpinner.gridy = 4;
		userInfo.add(playTimePanel, gbcPlayTimeSpinner);

		// Create Button
		JPanel createPanel = new JPanel(new BorderLayout());
		createBtn = new JButton("Create");
		createBtn.setEnabled(false);
		createBtn.addActionListener(this);
		createPanel.add(createBtn, BorderLayout.EAST);

		errorMessage = new JLabel("");
		errorMessage.setForeground(Color.RED);
		createPanel.add(errorMessage, BorderLayout.WEST);

		GridBagConstraints gbcCreatePanel = new GridBagConstraints();
		gbcCreatePanel.anchor = GridBagConstraints.EAST;
		gbcCreatePanel.gridx = 3;
		gbcCreatePanel.gridy = 6;
		userInfo.add(createPanel, gbcCreatePanel);

		return userInfo;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source.equals(createBtn) && e.getActionCommand().equals("Create")) {
			Child child = new Child();
			child.setName(nameField.getText());
			child.setPlayTimePerDay(((Integer) playTimeSpinner.getValue()).intValue());
			Sex sex = (Sex) sexComboBox.getSelectedItem();
			child.setSex(sex);
			child.setAge((Age) ageComboBox.getSelectedItem());
			if (sex == Sex.Female) {
				child.setImage(ParentSystemManager
						.getImageByFileName(ChildrenResource.imageGirlPrincess));
			} else {
				child.setImage(ParentSystemManager
						.getImageByFileName(ChildrenResource.imageBoy));
			}
			getMainFrame().getController(CreateChildController.class)
					.createChild(child);
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// Ignore this

	}

	@Override
	public void keyReleased(KeyEvent e) {
		Object source = e.getSource();
		if (source.equals(nameField)) {
			checkName();
		}

		// Check values
		if (userNamePassed) {
			createBtn.setEnabled(true);
		} else {
			createBtn.setEnabled(false);
		}

	}

	private void checkName() {
		String userName = nameField.getText();
		// Make sure field isn't empty
		if (!userName.equals("")) {
			errorMessage.setText("");
			userNamePassed = true;
			// Check if user already exists
		} else {
			errorMessage.setText("Username cannot be empty!");
			userNamePassed = false;
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// Ignore this
	}

	@Override
	public String getTitle() {
		return "Create New Child Account";
	}

	@Override
	public String getBorderTitle() {
		return "Create Child";
	}

}
